package rc.work.module.infra.util;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class ParseExcel {
    static List<RowRecord> parseExcel(Path file) throws IOException {
        List<RowRecord> list = new ArrayList<>();
        Workbook wb = WorkbookFactory.create(file.toFile());
        Sheet sheet = wb.getSheetAt(0);
        Row header = sheet.getRow(0);
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (row == null) continue;
            StringBuilder sb = new StringBuilder();
            for (int c = 0; c < row.getLastCellNum(); c++) {
                String col = header.getCell(c).toString();
                String val = row.getCell(c) == null ? "" : row.getCell(c).toString();
                sb.append(col).append(": ").append(val).append(" | ");
            }
            list.add(new RowRecord(i + 1, sb.toString()));   // Excel 行号从 1 起
        }
        return list;
    }

    record RowRecord(int rowNumber, String text) {}
}
